import java.util.Iterator;

import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smackx.Form;
import org.jivesoftware.smackx.FormField;
import org.jivesoftware.smackx.FormField.Option;
import org.jivesoftware.smackx.commands.LocalCommand;


public class MyLocalCommand extends LocalCommand {

	public static final String HUMANREADABLENAME = "My mock up command!";

	public static final int STAGE_FIRST = 0;
	public static final int STAGE_SECOND = 1;
	public static final int STAGE_THIRD = 2;
	public static final int STAGE_LAST = 3;

	public static final String FORMFIELD_PARAM1 = "my paramater 1";
	public static final String FORMFIELD_PARAM2 = "my paramater 2";

	public static final String FORMFIELD_LABEL_PARAM1 = "Give me the parameter 1!!!";
	public static final String FORMFIELD_LABEL_PARAM2 = "Give me the parameter 2!!!";


	public MyLocalCommand(){
		super();
	}

	@Override
	public boolean hasPermission(String jid) {
		System.out.println("hasPermission");
		//allow everyone
		return true;
	}

	@Override
	public boolean isLastStage() {
		System.out.println("isLastStage " + (getCurrentStage() == STAGE_LAST));
		return getCurrentStage() == STAGE_LAST;
	}

	@Override
	public void cancel() throws XMPPException {
		System.out.println("cancel");
	}

	@Override
	public void complete(Form response) throws XMPPException {
		System.out.println("complete");
		System.out.println("results from last stage");
		Iterator<FormField> iterFields = response.getFields();
		while(iterFields.hasNext()) {
			FormField field = iterFields.next();
			System.out.println("Variable "+field.getVariable()+": ");
			Iterator<String> iterString = field.getValues();
			while(iterString.hasNext()){
				String s = iterString.next();
				System.out.println(s);
			}
		}
	}

	@Override
	public void execute() throws XMPPException {
		System.out.println("execute");

		Form result = new Form(Form.TYPE_FORM);
		setExecuteAction(Action.next);

		FormField resultField = new FormField(FORMFIELD_PARAM1);
		resultField.setType(FormField.TYPE_TEXT_SINGLE);
		resultField.setLabel(FORMFIELD_LABEL_PARAM1);
		result.addField(resultField);

		this.addActionAvailable(Action.prev);
		this.addActionAvailable(Action.next);
		this.addActionAvailable(Action.complete);
		setForm(result);
	}

	@Override
	public void next(Form response) throws XMPPException {
		System.out.println("next - current stage: "+getCurrentStage());
	
		switch(getCurrentStage()){
		case STAGE_FIRST: {
			System.out.println("first stage");
			System.out.println("results from last stage");
			Iterator<FormField> iterFields = response.getFields();
			while(iterFields.hasNext()) {
				FormField field = iterFields.next();
				System.out.println("Variable "+field.getVariable()+": ");
				Iterator<String> iterString = field.getValues();
				while(iterString.hasNext()){
					String s = iterString.next();
					System.out.println(s);
				}
			}
			
			Form result = new Form(Form.TYPE_FORM);
			
			FormField resultField = new FormField(FORMFIELD_PARAM1);
			resultField.setType(FormField.TYPE_TEXT_SINGLE);
			resultField.setLabel(FORMFIELD_LABEL_PARAM1);
			result.addField(resultField);

			
			setForm(result);
			break;
		}
		case STAGE_SECOND: {
			System.out.println("second stage");
			System.out.println("results from last stage");
			Iterator<FormField> iterFields = response.getFields();
			while(iterFields.hasNext()) {
				FormField field = iterFields.next();
				System.out.println("Variable "+field.getVariable()+": ");
				Iterator<String> iterString = field.getValues();
				while(iterString.hasNext()){
					String s = iterString.next();
					System.out.println(s);
				}
			}
			Form result = new Form(Form.TYPE_FORM);
			FormField resultField = new FormField("my fixed field");
			resultField.setType(FormField.TYPE_FIXED);
			resultField.setLabel("lalalalala lalalalala");
			resultField.setDescription("description description description description description description description description ");
			result.addField(resultField);
			
			FormField resultField2 = new FormField("my bool field");
			resultField2.setType(FormField.TYPE_BOOLEAN);
			resultField2.setLabel("holodrio");
			resultField2.setRequired(true);
			result.addField(resultField2);
			
			FormField resultField3 = new FormField("my single choice list");
			resultField3.setType(FormField.TYPE_LIST_SINGLE);
			resultField3.addOption(new Option("label option1 ", "value option1"));
			resultField3.addOption(new Option("label option2 ", "value option2"));
			resultField3.addOption(new Option("label option3 ", "value option3"));
			resultField3.addOption(new Option("label option4 ", "value option4"));
			resultField3.setLabel("pick one");
			resultField3.setRequired(true);
			result.addField(resultField3);
			
			setForm(result);
			
			this.addActionAvailable(Action.prev);
			this.addActionAvailable(Action.next);
			this.addActionAvailable(Action.complete);

			break;
		}
		case STAGE_THIRD: { 
			System.out.println("third stage");
			System.out.println("results from last stage");
			Iterator<FormField> iterFields = response.getFields();
			while(iterFields.hasNext()) {
				FormField field = iterFields.next();
				System.out.println("Variable "+field.getVariable()+": ");
				Iterator<String> iterString = field.getValues();
				while(iterString.hasNext()){
					String s = iterString.next();
					System.out.println(s);
				}
			}
			
			
			Form result = new Form(Form.TYPE_FORM);
			
			FormField resultField = new FormField("results");
			resultField.setType(FormField.TYPE_FIXED);
			resultField.setLabel("nothing todo here");
			resultField.setDescription("description description description description description description description description ");
			result.addField(resultField);
			setForm(result);
			
			this.addActionAvailable(Action.prev);
			this.addActionAvailable(Action.next);
			this.addActionAvailable(Action.complete);
			
			break;
		}
		case STAGE_LAST: 	{
			System.out.println("last stage");
			System.out.println("results from last stage");
			Iterator<FormField> iterFields = response.getFields();
			while(iterFields.hasNext()) {
				FormField field = iterFields.next();
				System.out.println("Variable "+field.getVariable()+": ");
				Iterator<String> iterString = field.getValues();
				while(iterString.hasNext()){
					String s = iterString.next();
					System.out.println(s);
				}
			}
			
			this.addActionAvailable(Action.prev);
			this.addActionAvailable(Action.next);
			this.addActionAvailable(Action.complete);
			
			break;
		}
		}

		
	}

	@Override
	public void prev() throws XMPPException {
		System.out.println("prev - current stage: "+getCurrentStage());	
	}

}
